package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.IChannelProductRecommendDao;
import com.lianniu.mgr.cms.model.ChannelProductRecomendVo;
import com.lianniu.mgr.cms.service.IChannelProductRecomendService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ChannelProductRecomendServiceImpl implements	IChannelProductRecomendService {

	private final static Logger logger = Logger.getLogger(ChannelProductRecomendServiceImpl.class);
	  
	@Resource
	IChannelProductRecommendDao channelProductRecommendDao;
	
	@Override
	public BaseResult insertChannelProductRecomend(ChannelProductRecomendVo curChannelProductRecomendVo) {
		// TODO Auto-generated method stub
	    logger.info("insertChannelProductRecomend begin");
	    logger.info("insertChannelProductRecomend: input data check begin");
	    logger.info("insertChannelProductRecomend curChannelProductRecomendVo:" +JSON.toJSONString(curChannelProductRecomendVo));	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getChannelId())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getSectionId())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }  
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getChildSectionType())) {
	    	 baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		  }  
		    
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getSupplyProductId())) {
	    	 baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		     return baseResult;
		  }   
	    
	    logger.info("insertChannelProductRecomend: input data check end");	    
	    return BaseResult.success(channelProductRecommendDao.insertChannelProductRecommend(curChannelProductRecomendVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		// TODO Auto-generated method stub
		   logger.info("deleteById begin");
		   logger.info("deleteById Id:" +Id);
						  
		   return BaseResult.success(channelProductRecommendDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteChannelProductRecomendBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteChannelProductRecomendBatch begin");
	    logger.info("deleteChannelProductRecomendBatch Ids:" +id.toString());
	    logger.info("deleteChannelProductRecomendBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteChannelProductRecomendBatch: input data check end");
	    
	    return BaseResult.success(channelProductRecommendDao.deleteChannelProductRecommendBatch(id));
	}

	@Override
	public BaseResult updateById(ChannelProductRecomendVo curChannelProductRecomendVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curChannelProductRecomendVo:" +JSON.toJSONString(curChannelProductRecomendVo));	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getChannelId())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getChildSectionType())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getSectionId())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getSupplyProductId())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
	    if (StringUtil.isEmpty(curChannelProductRecomendVo.getSort())) {
	    	  curChannelProductRecomendVo.setSort("0");
	    }	    
	    logger.info("updateById: update data check end");	    
	    return BaseResult.success(channelProductRecommendDao.updateById(curChannelProductRecomendVo));
	}

	@Override
	public BaseResult selectByChannelProductRecomendForPage(ChannelProductRecomendVo curChannelProductRecomendVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByHelpCenterInfoForPage begin.");
	    if (!StringUtil.isEmpty(curChannelProductRecomendVo.getChannelName())) {
	    	curChannelProductRecomendVo.setChannelName(curChannelProductRecomendVo.getChannelName().replace(" ", ""));
	    }
	    logger.info("selectByHelpCenterInfoForPage Title:" +curChannelProductRecomendVo.getChannelName());
	
	    logger.info("selectByHelpCenterInfoForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curChannelProductRecomendVo.setPaginationInfo(paginationInfo);
	    List<ChannelProductRecomendVo> list=channelProductRecommendDao.selectByChannelProductRecommendForPage(curChannelProductRecomendVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByHelpCenterInfoForPage:  end");
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(ChannelProductRecomendVo curChannelProductRecomendVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curChannelProductRecomendVo.getId());
	  
	    return BaseResult.success(channelProductRecommendDao.selectById(curChannelProductRecomendVo.getId()));
	}



	
	
}
